package xyz.syyrjx.blog.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import xyz.syyrjx.blog.entity.*;
import xyz.syyrjx.blog.service.*;
import xyz.syyrjx.blog.util.MD5Util;
import xyz.syyrjx.blog.util.PageUtil;
import xyz.syyrjx.blog.util.StringUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
public class GuestController {

    private final Logger logger = LoggerFactory.getLogger(GuestController.class);

    @Resource
    private TypeService typeServer;
    @Resource
    private BlogService blogService;
    @Resource
    private UserService userService;
    @Resource
    private ToolService toolService;
    @Resource
    private CommentService commentService;

    private static final int SESSION_TIME = 5 * 60 * 60;

    @RequestMapping("/")
    public ModelAndView mainPage(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("main");
        return mv;
    }

    @GetMapping("/listBlogInPage")
    @ResponseBody
    public Result listBlogInPage(Integer page,Integer next,String search){
        search = StringUtil.searchDeal(search);
        int count = blogService.countBlogs(search,null);
        try {
            page = PageUtil.pageGet(count,page,next,5);
        }catch (Exception e) {
            return Result.fail("分页错误" + e);
        }
        List<Blog> blogList = blogService.likeQueryBlogInPage(page,search);
//        for (Blog blog: blogList){
//            long commentNum = commentService.countCommentByBlogId(blog.getId());
//            blog.setCommentNum(commentNum);
//        }
        return Result.success(blogList);
    }

    @GetMapping("/listAllType")
    @ResponseBody
    public Result listAllType(){
        List<Types> typesList = typeServer.queryAllTypes();
        return Result.success(typesList);
    }

    @RequestMapping("/countAndPage")
    @ResponseBody
    public Result countAndPage(Integer page,Integer next,String search){
        search = StringUtil.searchDeal(search);
        int count = blogService.countBlogs(search,null);
        List<Integer> pageList = PageUtil.pageListGet(count,page,next,5);
        return Result.success(pageList);
    }

    @RequestMapping("/countAndPageTools")
    @ResponseBody
    public Result countAndPageTools(Integer page,Integer next){
        int count = toolService.countTool();
        List<Integer> pageList = PageUtil.pageListGet(count,page,next,5);
        return Result.success(pageList);
    }

    @RequestMapping("/listToolsInfo")
    @ResponseBody
    public Result listToolsInfo(Integer page,Integer next){
        int count = toolService.countTool();
        page = PageUtil.pageGet(count,page,next,5);
        return Result.success(toolService.queryToolInPage(page));
    }

    @GetMapping("/tool")
    public ModelAndView toolPage(Integer page,Integer next){
        ModelAndView mv = new ModelAndView();




        mv.setViewName("tool");
        return mv;
    }

    @RequestMapping("/blog")
    public ModelAndView blog(){
        ModelAndView mv =new ModelAndView();
        mv.setViewName("blog");
        return mv;
    }

    @GetMapping("/getBlog")
    @ResponseBody
    public Result getBlog(Long id, HttpSession session){
        Blog blog = blogService.queryAndConvert(id);
        blog.setTypes(typeServer.queryTypeById(blog.getType()));
        String str = "blog"+blog.getId();
        if(session.getAttribute(str) == null && session.getAttribute("user") == null){
            session.setAttribute(str,1);
            if(blogService.updateView(blog)){
                blog.setView(blog.getView()+1);
            }
        }
        return Result.success(blog);
    }

    @GetMapping("/getCommentList")
    @ResponseBody
    public Result getCommentList(Long id){
        return Result.success(commentService.queryCommentByBlogId(id));
    }

    @RequestMapping("/getBlogPageInType")
    @ResponseBody
    public Result countBlogInType(Integer page,Integer next,Long typeId){
        int count = blogService.countBlogs(null,typeId);
        page = PageUtil.pageGet(count,page,next,5);
        return Result.success(page);
    }

    @RequestMapping("/getBlogByType")
    @ResponseBody
    public Result getBlogByType(Integer page,Integer next,Long typeId){
        int count = blogService.countBlogs(null,typeId);
        page = PageUtil.pageGet(count,page,next,5);
        List<Blog> blogList = blogService.queryBlogByTypeInPage(page, typeId);
//        for (Blog blog: blogList){
//            long commentNum = commentService.countCommentByBlogId(blog.getId());
//            blog.setCommentNum(commentNum);
//        }
        return Result.success(blogList);
    }

    @RequestMapping("/getTypeList")
    @ResponseBody
    public Result getTypeList(){
        List<Types> typeList = typeServer.queryAllTypes();
        return Result.success(typeList);
    }

    @RequestMapping("/types")
    public ModelAndView types(Integer page,Integer next,Long typeId){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("types");
        return mv;
    }


    @RequestMapping("/archives")
    public ModelAndView archives(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("archives");
        return mv;
    }

    @GetMapping("/getArchives")
    @ResponseBody
    public Result getArchives(){
        return Result.success(blogService.getArchives());
    }

    @RequestMapping("/about")
    public String about(){
        return "about";
    }


    @RequestMapping("/login")
    public String login(HttpSession session){
        String path = "/admin/hello";
        if (session.getAttribute("user") == null){
            path = "login";
        }
        return path;
    }

    @PostMapping("/checkUser")
    public String checkUser(String username, String password, HttpSession session){
        String password_MD5 = MD5Util.code(password);
        User user = userService.queryUserByLogin(username,password_MD5);
        if(user == null){
            return "login";
        }else{
            user.setPassword("****");
            session.setAttribute("user",user);
            session.setMaxInactiveInterval(SESSION_TIME);
            return "admin/hello";
        }
    }

    @PostMapping("/addComment")
    public String addComment(Comment comment,HttpSession session){
        comment.setCreate_date(new Date());
        if (session.getAttribute("user") != null){
            comment.setAuthor("syyrjx");
            comment.setHead("images/MyHead.jpg");
        }
        commentService.addComment(comment);
        return "redirect:/showNewComment/" + comment.getBlogId();
    }

    @PostMapping("/delComment")
    public String delComment(Long id,HttpSession session){
        long blogId = commentService.queryCommentById(id).getBlogId();
        if (session.getAttribute("user") != null){
            commentService.deleteCommentTree(id);
        }
        return "redirect:/showNewComment/" + blogId;
    }

    @RequestMapping("/showNewComment/{blogId}")
    public String showNewComment(@PathVariable Long blogId, Model model){
        List<Comment> commentList = commentService.queryCommentByBlogId(blogId);
        model.addAttribute("commentList",commentList);
        return "blog::commentList";
    }





}
